/*******************************************************************************
 * Copyright (c) 2008, 2009 Fabio Mauro - Alberto Terraneo.
 * Permission is granted to copy, distribute and/or modify this document
 * under the terms of the GNU Free Documentation License, Version 1.2
 * or any later version published by the Free Software Foundation.
 *     
 * This program is distributed in the hope that it will be useful, 
 * but WITHOUT ANY WARRANTY; without even the implied 
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
 * See the GNU Free Documentation License for more details.
 *     
 * You should have received a copy of the GNU Free Documentation 
 * License along with this distribution; if not, send a email to authors:
 * aterraneo <at> users.sourceforge.net
 * bigmoby09 <at> users.sourceforge.net
 *******************************************************************************/
package it.blueocean.acanto.web.pages.secure.prodotto;

import it.blueocean.acanto.model.Prodotto;
import it.blueocean.acanto.model.constants.StatoProdotto;
import it.blueocean.acanto.model.constants.TipoIva;
import it.blueocean.acanto.model.constants.TipoUnitaDiMisura;
import it.blueocean.acanto.service.ProdottoService;
import it.blueocean.acanto.web.platform.components.feedback.ValidationStyleBehavior;
import it.blueocean.acanto.web.platform.components.panel.AcantoPanel;

import java.math.BigDecimal;
import java.util.List;

import org.apache.wicket.Component;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxFallbackButton;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.FormComponent;
import org.apache.wicket.markup.html.form.IFormVisitorParticipant;
import org.apache.wicket.markup.html.form.TextArea;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.LoadableDetachableModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.apache.wicket.util.value.ValueMap;
import org.apache.wicket.validation.validator.RangeValidator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class NuovoProdotto extends AcantoPanel
{
	private static final long serialVersionUID = 2022239405641534350L;

	private static final Logger log = LoggerFactory.getLogger(NuovoProdotto.class);

	private Label name;

	@SpringBean
	private ProdottoService prodottoService;

	public NuovoProdotto(String id)
	{
		super(id);

		name = new Label("name", "nuovo prodotto");
		name.setOutputMarkupId(true);
		add(name);

		add(getComposedForm());
	}

	private Component getComposedForm()
	{
		@SuppressWarnings("rawtypes")
		AcantoStandardForm form = new AcantoStandardForm("acantoStandardForm");
		form.setOutputMarkupId(true);

		TextField<String> codiceProdotto = new TextField<String>("codiceProdotto");
		codiceProdotto.setRequired(true);
		codiceProdotto.setOutputMarkupId(true);
		codiceProdotto.add(new ValidationStyleBehavior());
		form.add(codiceProdotto);

		TextField<Double> prezzoUnitario = new TextField<Double>("prezzoUnitario",Double.class);
		prezzoUnitario.setRequired(true);
		prezzoUnitario.setOutputMarkupId(true);
		prezzoUnitario.add(new RangeValidator<Double>(0d, 99999999d));
		prezzoUnitario.add(new ValidationStyleBehavior());
		form.add(prezzoUnitario);

		TextField<Double> scontoPercentuale = new TextField<Double>("scontoPercentuale",Double.class);
		scontoPercentuale.setRequired(true);
		scontoPercentuale.setOutputMarkupId(true);
		prezzoUnitario.add(new RangeValidator<Double>(0d, 99999999d));
		scontoPercentuale.add(new ValidationStyleBehavior());
		form.add(scontoPercentuale);

		DropDownChoice<String> aliquotaIvaComboBox = new DropDownChoice<String>("aliquotaIva",
				new LoadableDetachableModel<List<String>>()
				{
					private static final long serialVersionUID = -8189203261004116433L;

					@Override
					protected List<String> load()
					{
						return TipoIva.getListStringTipiIvaValues();
					}

				});
		aliquotaIvaComboBox.setRequired(true);
		aliquotaIvaComboBox.setOutputMarkupId(true);
		aliquotaIvaComboBox.add(new ValidationStyleBehavior());
		form.add(aliquotaIvaComboBox);

		DropDownChoice<String> unitaMisuraComboBox = new DropDownChoice<String>("unitaMisura",
				new LoadableDetachableModel<List<String>>()
				{

					private static final long serialVersionUID = 3712289549796409053L;

					@Override
					protected List<String> load()
					{
						return TipoUnitaDiMisura.getListStringTipiUnitadiMisuraValues();
					}
				});
		unitaMisuraComboBox.setRequired(true);
		unitaMisuraComboBox.add(new ValidationStyleBehavior());
		unitaMisuraComboBox.setOutputMarkupId(true);
		form.add(unitaMisuraComboBox);

		TextArea<String> descrizione = new TextArea<String>("descrizione");
		descrizione.setRequired(true);
		descrizione.setOutputMarkupId(true);
		descrizione.add(new ValidationStyleBehavior());
		form.add(descrizione);

		TextField<String> note = new TextField<String>("note");
		note.setOutputMarkupId(true);
		form.add(note);

		DropDownChoice<String> statusComboBox = new DropDownChoice<String>("status",
				new LoadableDetachableModel<List<String>>()
				{
					private static final long serialVersionUID = 2933800918053929315L;

					@Override
					protected List<String> load()
					{
						return StatoProdotto.getListStringStatiProdottoValues();
					}
				});
		statusComboBox.setRequired(true);
		statusComboBox.add(new ValidationStyleBehavior());
		statusComboBox.setOutputMarkupId(true);
		form.add(statusComboBox);

		form.add(new AjaxFallbackButton("submitButton", form)
		{
			private static final long serialVersionUID = 159710398094370478L;

			@Override
			protected void onSubmit(AjaxRequestTarget target, Form<?> form)
			{
				ValueMap values = (ValueMap) form.getDefaultModelObject();

				String codiceProdotto = values.getString("codiceProdotto");
				Double prezzoUnitario = values.getDouble("prezzoUnitario");
				Double scontoPercentuale = values.getDouble("scontoPercentuale");
				String aliquotaIva = values.getString("aliquotaIva");
				String unitaMisura = values.getString("unitaMisura");
				String descrizione = values.getString("descrizione");
				String note = values.getString("note");
				String status = values.getString("status");

				log.debug("saving new Prodotto");

				Prodotto prodotto = new Prodotto();
				prodotto.setAliquotaIva(TipoIva.getFromValue(aliquotaIva));
				prodotto.setCodiceProdotto(codiceProdotto);
				prodotto.setDescrizione(descrizione);
				prodotto.setNote(note);
				prodotto.setPrezzoUnitario(new BigDecimal(prezzoUnitario));
				prodotto.setScontoPercentuale(new Double(scontoPercentuale));
				prodotto.setStatus(StatoProdotto.getFromValue(status));
				prodotto.setUnitaMisura(TipoUnitaDiMisura.getFromValue(unitaMisura));

				prodottoService.store(prodotto);

				getSuccessFeedbackModalWindow().show(target);
			}

			@Override
			protected void onError(final AjaxRequestTarget target, Form<?> form)
			{
				form.visitFormComponents(new IVisitor()
				{
					public Object formComponent(IFormVisitorParticipant formVisitor)
					{
						if(formVisitor instanceof FormComponent)
						{
							FormComponent<?> formComponent = (FormComponent<?>) formVisitor;
							target.addComponent(formComponent);
						}
						return formVisitor;
					}
				});

				getErrorFeedbackModalWindow().show(target);
			}
		});

		return form;
	}

}
